package com.es.test;

import com.es.entity.SysRole;
import com.es.entity.SysUser;
import com.es.service.SysRoleService;
import com.es.service.SysUserService;
import com.es.service.impl.SysRoleServiceImpl;
import com.es.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

import javax.management.relation.Role;
import java.util.List;

/**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-10 10:56
 */
@Slf4j
public class UserServlet {
    SysUserService userService;
    SysRoleService roleService;
    @Before
    public void before(){
        userService = new SysUserServiceImpl();
        roleService = new SysRoleServiceImpl();
    }

    @Test
    public void saveRoles(){
        SysRole role = new SysRole("CEO","首席执行官");
        SysRole role1 = new SysRole("CTO","首席技术官");
        SysRole role2 = new SysRole("CFO","首席财务官");
        //
        roleService.save(role);
        roleService.save(role2);
        roleService.save(role1);
    }

    @Test
    public void saveUsers(){
        SysUser sysUser = new SysUser("admin","admin","超级管理员");
        SysUser sysUser1 = new SysUser("guest","guest","来访账户");
        SysUser sysUser2 = new SysUser("root","root","根账户");

        userService.save(sysUser);
        userService.save(sysUser1);
        userService.save(sysUser2);

    }

    @Test
    public void grantRoles(){
        //
        SysUser sysUser = userService.get(2);
        log.info("查询到的用户是：{}",sysUser);
        //
        List<SysRole> roles = roleService.list();
        //
        sysUser.setRoles(roles);
        userService.grantRoles(sysUser);

    }

    @Test
    public void get(){
        SysUser user = userService.get(2);
        //System.out.println(user.getRoles());
        System.out.println("当前用户是："+user.getName());
        System.out.println("\t他的角色有：");
        System.out.println("\t角色编号\t角色名称");
        user.getRoles().forEach(r->{
            System.out.println("\t"+r.getId()+"\t"+r.getName());
        });
    /*    //
        user.getRoles().remove(2);
        userService.grantRoles(user);*/
    }
}
